package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.DimBranch;
import com.excesys.exsecs.energy.service.DimBranchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能耗维度：支路支路中不支持其他选项
 *
 * @author lxc
 * @date 2024-03-04 14:10:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dimBranch" )
@Tag(description = "dimBranch" , name = "能耗维度：支路支路中不支持其他选项管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DimBranchController {

    private final DimBranchService dimBranchService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dimBranch 能耗维度：支路支路中不支持其他选项
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_view')" )
    public R getDimBranchPage(@ParameterObject Page page, @ParameterObject DimBranch dimBranch) {
        LambdaQueryWrapper<DimBranch> wrapper = Wrappers.lambdaQuery();
        return R.ok(dimBranchService.page(page, wrapper));
    }

	/**
	 * 获取支路树
	 * @return
	 */
	@Operation(summary = "获取支路树" , description = "获取支路树" )
	@GetMapping("/tree" )
	@PreAuthorize("@pms.hasPermission('twinops_dimBranch_view')" )
	public R getTree(@ParameterObject DimBranch dimBranch) {
		return R.ok(dimBranchService.queryTree(dimBranch));
	}


    /**
     * 通过id查询能耗维度：支路支路中不支持其他选项
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(dimBranchService.getBranchById(id));
    }

    /**
     * 新增能耗维度：支路支路中不支持其他选项
     * @param dimBranch 能耗维度：支路支路中不支持其他选项
     * @return R
     */
    @Operation(summary = "新增能耗维度：支路" , description = "新增能耗维度：支路" )
    @SysLog("新增能耗维度：支路" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_add')" )
    public R save(@RequestBody DimBranch dimBranch) {
        return R.ok(dimBranchService.save(dimBranch));
    }

	/**
	 * 关联表计保存
	 * @return R
	 */
	@Operation(summary = "关联表计保存" , description = "关联表计保存" )
	@SysLog("关联表计保存" )
	@PostMapping("/saveDevBranchMap")
	@PreAuthorize("@pms.hasPermission('twinops_dimBranch_add')" )
	public R saveDevBranchMap(@RequestBody DimBranch dimBranch) {
		return R.ok(dimBranchService.saveDevBranchMap(dimBranch));
	}

	/**
	 * 通过parentId查询关联的表计信息
	 * @param dimBranch dimBranch
	 * @return R
	 */
	@Operation(summary = "通过parentId查询关联的表计信息" , description = "通过parentId查询关联的表计信息" )
	@GetMapping("/meter" )
	@PreAuthorize("@pms.hasPermission('twinops_dimBranch_view')" )
	public R getMetersById(@ParameterObject DimBranch dimBranch) {
		return R.ok(dimBranchService.getMetersById(dimBranch));
	}

    /**
     * 修改能耗维度：支路支路中不支持其他选项
     * @param dimBranch 能耗维度：支路支路中不支持其他选项
     * @return R
     */
    @Operation(summary = "修改能耗维度：支路" , description = "修改能耗维度：支路" )
    @SysLog("修改能耗维度：支路" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_edit')" )
    public R updateById(@RequestBody DimBranch dimBranch) {
        return R.ok(dimBranchService.updateById(dimBranch));
    }

    /**
     * 通过id删除能耗维度：支路支路中不支持其他选项
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除能耗维度：支路" , description = "通过id删除能耗维度：支路" )
    @SysLog("通过id删除能耗维度：支路" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(dimBranchService.removeBranchBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param dimBranch 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('twinops_dimBranch_export')" )
    public List<DimBranch> export(DimBranch dimBranch,Long[] ids) {
        return dimBranchService.list(Wrappers.lambdaQuery(dimBranch).in(ArrayUtil.isNotEmpty(ids), DimBranch::getId, ids));
    }
}